using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class UrlRewriteModule : System.Web.IHttpModule 
{ 
    public void Dispose()
    {  
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new System.EventHandler(Rewrite_BeginRequest);
    }

    public void Rewrite_BeginRequest(object sender, System.EventArgs args)
    {        
        string path = HttpContext.Current.Request.Url.AbsolutePath;
        UrlRedirection urlRedirection = new UrlRedirection();

        string url = path;
        string rewrite = urlRedirection.GetMatchingRewrite(path);
        if (!String.IsNullOrEmpty(rewrite))
        {
            url = rewrite;
        }
        else
        {
            url = path;
        }
        if (!path.Equals(url))
        {            
            HttpContext.Current.RewritePath("~" + url);
        }
    }   
}
